package dync.rtmpc_demo;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.anyrtc.rtmpc_hybrid.RTMPCHybird;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dync.rtmpc_demo.guest.GuestActivity;
import dync.rtmpc_demo.hoster.HosterActivity;
import dync.rtmpc_demo.model.LiveBean;
import dync.rtmpc_demo.utils.Constans;
import dync.rtmpc_demo.utils.RTMPCHttpSDK;
import dync.rtmpc_demo.utils.ToastUtil;

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.rv_live_list)
    RecyclerView rvLiveList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.tv_host)
    TextView tvHost;
    LiveListAdapter mAdapter;
    List<LiveBean> liveBeanList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        swipeRefresh.setOnRefreshListener(this);
        mAdapter=new LiveListAdapter();
        mAdapter.setOnItemClickListener(this);
        rvLiveList.setLayoutManager(new LinearLayoutManager(this));
        rvLiveList.setAdapter(mAdapter);
//        GetLiveListData();

    }

    private void GetLiveListData() {
        RTMPCHttpSDK.GetLiveList(this, RTMPCHybird.Inst().GetHttpAddr(), Constans.DEVELOPERID, Constans.APPID, Constans.APPTOKEN, new RTMPCHttpSDK.RTMPCHttpCallback() {
            @Override
            public void OnRTMPCHttpOK(String strContent) {
                swipeRefresh.setRefreshing(false);
                liveBeanList.clear();
                Log.d("result=", strContent + "");
                try {
                    JSONObject liveJson = new JSONObject(strContent);
                    JSONArray liveList = liveJson.getJSONArray("LiveList");
                    for (int i = 0; i < liveList.length(); i++) {
                        JSONObject itemJson = new JSONObject(liveList.getString(i));
                        LiveBean bean = new LiveBean(itemJson.getString("hosterId"),itemJson.getString("topic"),itemJson.getString("rtmp_url"),itemJson.getString("anyrtcId"),itemJson.getBoolean("isAudioOnly"));
                        liveBeanList.add(bean);
                    }
                    mAdapter.setNewData(liveBeanList);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void OnRTMPCHttpFailed(int code) {
                swipeRefresh.setRefreshing(false);
                ToastUtil.show("获取链表失败 code="+code);
            }
        });
    }

    @Override
    public void onRefresh() {
        GetLiveListData();
    }

    @OnClick(R.id.tv_host)
    public void onClick() {
        startActivity(new Intent(MainActivity.this, HosterActivity.class));
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent i=new Intent(MainActivity.this, GuestActivity.class);
        i.putExtra("pull_url", mAdapter.getItem(position).Rtmp_url);
        i.putExtra("anyrtcid", mAdapter.getItem(position).AnyrtcId);
        startActivity(i);
    }
}
